//
//  HomeViewController.m
//  预购清单
//
//  Created by  on 2022/10/15.
//

#import "HomeViewController.h"
#import "HomeHeader.h"
#import "HomeFooter.h"
#import "PayCell.h"
#import "PayViewController.h"
#import "BuyViewController.h"
#import "DetailViewController.h"

@interface HomeViewController ()
@property (strong, nonatomic) IBOutlet UIButton *addBtn;
@property (nonatomic,assign) NSInteger stamp;
@property (nonatomic, strong) Budget *model;
@property (nonatomic, assign) CGFloat totalmoney;
@end

@implementation HomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self addNotify];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd"] integerValue];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)initUI
{
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[PayCell nib] forCellReuseIdentifier:[PayCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(0);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(0);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(0);
    self.tableView.c_bottom.equalTo(self.view.c_bottom).coffset(0);
    [self.view bringSubviewToFront:self.addBtn];
}

- (void)loadData{
    [BudgetTool find:APP.user.account stamp:self.stamp complete:^(Budget * _Nonnull budget) {
        self.model = budget;
        [PrepayTool find:budget complete:^(NSMutableArray<Prepay *> * _Nonnull result) {
            self.model.array = result;
            CGFloat  waitpay = 0;
            CGFloat pay = 0;
            for (Prepay *model in result) {
                waitpay = model.payed ? waitpay : waitpay + model.money;
                pay = model.payed ? pay + model.money : pay;
            }
            self.model.waitPayed = waitpay;
            self.model.payed = pay;
            [self.tableView reloadData];
        }];
    }];
}
#pragma mark - 支付结算
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.model.array.count == 0) {
        return [self alert:LS(@"注意") :LS(@"当前无记录，先去添加预购清单吧") :nil :^{
            BuyViewController *vc = [BuyViewController new];
            [[self currentVC] push:vc :NO];
        }];
    }
    PayViewController *vc = [PayViewController new];
    vc.money.text = [NSString stringWithFormat:@"%@%@%0.2f",LS(@"总计："),LS(@"¥"),self.totalmoney];
    vc.block = ^{
        for (Prepay *model in self.model.array) {
            if (model.payed == NO && model.select == YES) {
                model.payed = YES;
                model.select = NO;
            }
        }
        self.totalmoney = 0;
        [BudgetTool update:self.model complete:^(BOOL success) {
            [NotifyTool post:notifyBudgetUpdate :nil];
        }];
    };
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)setStamp:(NSInteger )stamp{
    _stamp = stamp;
    [self loadData];
}
- (void)addNotify{
    [super addNotify];
    [NotifyTool add:self :@selector(loadData) :notifyPayUpdate :nil];
    [NotifyTool add:self :@selector(loadData) :notifyBudgetUpdate :nil];
    [NotifyTool add:self :@selector(loadData) :notifyUserUpdate :nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PayCell *cell = [tableView dequeueReusableCellWithIdentifier:[PayCell ID]];
    Prepay *model = self.model.array[indexPath.row];
    cell.model = model;
    WS(self)
    cell.block = ^{
        weakself.totalmoney = model.select ? weakself.totalmoney + model.money : weakself.totalmoney - model.money;
        [weakself.tableView reloadData];
    };
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.model.array.count == 0 ? 250 : 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 289;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.model.array.count > 0) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    view.backgroundColor = [UIColor clearColor];
    HomeFooter *footer = (HomeFooter *)[HomeFooter loadFromXIB];
    footer.frame = view.bounds;
    [view addSubview:footer];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 289)];
    view.backgroundColor = [UIColor clearColor];
    HomeHeader *header = (HomeHeader *)[HomeHeader loadFromXIB];
    header.frame = view.bounds;
    header.model = self.model;
    header.block = ^{
        PickViewController *vc = [PickViewController new];
        vc.stamp = [[NSDate currentTimeStamp:@"yyyy-MM"] intValue];;
        vc.stampBlock = ^(NSInteger stamp) {
            self.stamp = stamp;
        };
        [vc show];
    };
    [view addSubview:header];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *vc = [DetailViewController new];
    vc.model = self.model.array[indexPath.row];
    [self push:vc :YES];
}

@end

